package com.loan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.Result;
import com.loan.entity.User;
import com.loan.mapper.UserMapper;
import com.loan.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2021-09-11
 */
@Api(tags = "UserController",description = "用户管理")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    UserMapper userMapper;

    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userName",dataType = "String",required = true,value = "用户的登录账号",defaultValue = "admin"),
            @ApiImplicitParam(paramType="query",name = "password",dataType = "String",required = true,value = "用户的密码",defaultValue = "admin")
    })
    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public Result login(String userName, String password){


        System.out.println(userName);
        System.out.println(password);
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("userName",userName);
        columnMap.put("password",password);
        List<User> userList = userMapper.selectByMap(columnMap);

        Result result = new Result();
        if (userList.size()>0){

            User user = new User();
            user.setUserName(userList.get(0).getUserName());
            user.setUid(userList.get(0).getUid());
            redisTemplate.opsForValue().set("userName",userList.get(0).getUserName());
            redisTemplate.opsForValue().set("uid",userList.get(0).getUid());
            user.setPassword(userList.get(0).getPassword());
            result.setSuccess(true);
            result.setObj(user);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    /**
     * 修改用户
     * @param password
     * @return
     */
    @ApiOperation("修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userName",dataType = "String",required = true,value = "用户的登录账号",defaultValue = "admin"),
            @ApiImplicitParam(paramType="query",name = "password",dataType = "String",required = true,value = "用户的密码",defaultValue = "admin")
    })
    @PostMapping("/user/updateuser")
    public Result update(String password){

        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        User user = new User();
        user.setPassword(password);
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("uid",uid);
        int count = userMapper.update(user, wrapper);
        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @PostMapping("/user/updateuserByUid")
    public Result updateuserByUid(String userName,String sex,String age,String IdCard,String email,String phone){

        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;

        User user = new User();
        user.setUserName(userName);
        user.setSex(sex);
        user.setAge(Integer.valueOf(age));
        user.setIdCard(IdCard);
        user.setEmail(email);
        user.setPhone(phone);
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("uid",uid);
        int count = userMapper.update(user, wrapper);
        Result result = new Result();
        if (count>0){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    /**
     * 添加用户操作
     * @param user
     * @return
     */
    @ApiOperation("添加用户操作")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "userName",dataType = "String",required = true,value = "用户的登录账号",defaultValue = "admin"),
            @ApiImplicitParam(paramType="query",name = "password",dataType = "String",required = true,value = "用户的密码",defaultValue = "admin")
    })
    @PostMapping("/user/adduser")
    public Boolean save(User user){
        return userService.save(user);
    }

    /**
     * 根据uid查询用户信息
     * @param
     * @return
     */
    @ApiOperation("根据uid查询用户信息")
    @ApiImplicitParam(paramType="query",name = "uid",dataType = "Integer",required = true,value = "用户的id",defaultValue = "1")
    @GetMapping("/user/getUserByUid")
    private User getuserByUid(){
        Object uids = redisTemplate.opsForValue().get("uid");
        Integer uid = (Integer) uids;
        return userService.getById(uid);
    }

    @ApiOperation("查询用户列表")
    @GetMapping("/user/getUserList")
    private List<User> getUserList(){
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    /**
     * 用邮箱接收验证码
     * @param email
     * @return
     */
    @ApiOperation("用邮箱接收验证码")
    @ApiImplicitParam(paramType="query",name = "email",dataType = "String",required = true,value = "用户的邮箱")
    @RequestMapping(value = "/user/sendMail",method = RequestMethod.POST)
    public Result sendMail(String email){
        Result result = new Result();
        System.out.println(email);
        if (email==null){
            result.setSuccess(false);
            return result;
        }
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        try {
            //邮件发送人
            simpleMailMessage.setFrom("V17695662538@126.com");
            //邮件接收人
            simpleMailMessage.setTo(email);
            //邮件主题
            simpleMailMessage.setSubject("主题：验证码提醒");
            //产生随机的6位数密码
            int Code = ((int)((Math.random()*9+1)*100000));
            //邮件内容
            simpleMailMessage.setText("xxx提醒，您的验证码为："+ Code);
            javaMailSender.send(simpleMailMessage);

            result.setSuccess(true);
            result.setMsg(String.valueOf(Code));
            return result;
        } catch (Exception e) {
            System.out.println("邮件发送失败:"+e.getMessage());
            e.printStackTrace();
        }
        result.setSuccess(false);
        return result;

    }
}

